package org.dftproject.genesis.ui.agents.pages;

import org.dftproject.genesis.ui.agents.Activator;
import org.dftproject.genesis.ui.figures.FigureCanvasEx;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.dftproject.genesis.ui.pages.AbstractPageInput;
import org.dftproject.genesis.ui.pages.AbstractPagePart;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPageSite;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.PageUtils;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class NewAgentPage extends AbstractPagePart {

	public static final String ID = NewAgentPage.class.getName();

	private FigureCanvas canvas;

	public void init(IPageSite site, IPageInput input) throws PageInitException {
		this.setSite(site);
		this.setInput(input);
	}

	@Override
	public void createPartControl(Composite parent) {
		createContents(parent);
	}

	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvasEx(parent);
		canvas.getViewport().setContentsTracksWidth(true);

		// Create the root figure

		Figure container = new PageContainer();
		canvas.setContents(container);

		container.add(PageUtils.createH1Label("Start an Agent"));

		container.add(PageUtils.createFlowedText("Genesis can automatically perform the following research and analysis tasks:"));

		IFigure linkContainer = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		linkContainer.setLayoutManager(layout);
		linkContainer.setBorder(new MarginBorder(10, 10, 0, 0));
		container.add(linkContainer);

		IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(Activator.ID, Activator.AGENT_HANDLERS_EXTENSION_POINT);
		for (IExtension extension : extensionPoint.getExtensions()) {
			for (IConfigurationElement configElement : extension.getConfigurationElements()) {
				final String newPageId = configElement.getAttribute("newPageId");
				if (newPageId != null) {
					final String name = configElement.getAttribute("name");

					HyperlinkFigure link = new HyperlinkFigure(name);
					link.addHyperlinkListener(new IHyperlinkListener() {

						public void linkActivated(Object data, int stateMask) {
							IWorkbench workbench = PlatformUI.getWorkbench();
							IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
							IWorkbenchPage workbenchPage = window.getActivePage();
							PageManager.getDefault().openPage(workbenchPage, new AbstractPageInput() {

								{
									setLabel(name);
									setImageDescriptor(Activator.getImageDescriptor("icons/cog_add.png"));
								}

							}, newPageId, true, (stateMask & SWT.CTRL) != 0);
						}

					});
					linkContainer.add(link);
				}
			}
		}
	}

	@Override
	public void dispose() {
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
